package com.chenl.provider.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;


//@Component
public class StartupBeanPrinter implements CommandLineRunner {

    @Autowired
    private ApplicationContext applicationContext;

//    @Override
//    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//        this.applicationContext = applicationContext;
//    }

    @Override
    public void run(String... args) throws Exception {
        // 获取所有 Bean 名称
        String[] beanNames = applicationContext.getBeanDefinitionNames();
        
        // 打印所有 Bean 名称
        System.out.println("All Beans in Spring Context:");
        for (String name : beanNames) {
            System.out.println(name);
        }
        
        // 检查某个 Bean 是否存在
        String beanNameToCheck = "yourBeanName";
        if (applicationContext.containsBean(beanNameToCheck)) {
            System.out.println("Bean '" + beanNameToCheck + "' exists in Spring Context.");
        } else {
            System.out.println("Bean '" + beanNameToCheck + "' does not exist in Spring Context.");
        }
    }
}